package org.nnxy.foodboot.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.nnxy.foodboot.entity.Recipe;
import org.nnxy.foodboot.entity.UserViewHistory;

/**
 * 用户浏览历史Mapper接口
 */
@Mapper
public interface UserViewHistoryMapper extends BaseMapper<UserViewHistory> {
    
    /**
     * 分页查询用户浏览历史
     * @param page 分页参数
     * @param userId 用户ID
     * @return 分页结果
     */
    @Select("SELECT r.*, uvh.view_duration, uvh.create_time as view_time FROM recipe r " +
            "INNER JOIN user_view_history uvh ON r.id = uvh.recipe_id " +
            "WHERE uvh.user_id = #{userId} AND r.del_flag = '0' " +
            "ORDER BY uvh.create_time DESC")
    IPage<Recipe> selectUserViewHistoryRecipes(Page<Recipe> page, @Param("userId") Long userId);
    
    /**
     * 查询用户对特定食谱的浏览历史
     * @param userId 用户ID
     * @param recipeId 食谱ID
     * @return 浏览历史
     */
    @Select("SELECT * FROM user_view_history " +
            "WHERE user_id = #{userId} AND recipe_id = #{recipeId} " +
            "ORDER BY create_time DESC LIMIT 1")
    UserViewHistory getUserLatestViewHistory(@Param("userId") Long userId, @Param("recipeId") Long recipeId);
} 